package com.uniview.service.orderType.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.uniview.entity.vo.Pager;
import com.uniview.entity.vo.orderType.Rma;
import com.uniview.plugins.adapter.OLDataSource;
import com.uniview.service.orderType.RmaService;

@Service("rmaService")
public class RmaServiceImpl implements RmaService {

    @Value("#{configProperties['uniview.Rma']}")
    private String RmaTable;

    @Value("#{configProperties['uniview.Operator']}")
    private String operatorTable;

    @Value("#{configProperties['uniview.OrderService']}")
    private String RmaOrderTbale;

    @Value("#{configProperties['uniview.OperatorCustomer']}")
    private String RmaOperatorCustomerTable;

    @Value("#{configProperties['uniview.Materials']}")
    private String RmaMaterialsTable;

    @Autowired
    private OLDataSource olDataSource;

    @Override
    public Pager<Rma> findRmaByWorkNo(String workNo, String queryString, int page, int limit) throws Exception {
        workNo = workNo.substring(1);
        StringBuilder builder = new StringBuilder();
        builder.append("select * from (select A.*,ROWNUM as id FROM ( ");
        builder.append("select * from " + RmaTable + " where work_num = '" + workNo + "'");
        if (StringUtils.isNotBlank(queryString)) {
            builder.append(" and (customer_name like '%" + queryString + "%' or code_confirm like '%" + queryString + "%')");
        }
        builder.append(" ) A WHERE ROWNUM <= " + page + " * " + limit + " ) WHERE id > (" + page + " - 1) *" + limit);
        String countSql = "select count(*) from " + RmaTable + " where work_num = '" + workNo + "'";
        if (StringUtils.isNotBlank(queryString)) {
            countSql += " and (customer_name like '%" + queryString + "%' or code_confirm like '%" + queryString + "%')";
        }
        List<Rma> list = olDataSource.queryForList(Rma.class, builder.toString());
        int total = olDataSource.getCount(countSql);
        Pager<Rma> pager = new Pager<Rma>();
        pager.setPagerList(list);
        pager.setTotalRow(total);
        return pager;
    }

    @Override
    public Pager<Rma> findRmaOrdersByWorkNo(String operatorId, String queryString, int page, int limit) throws Exception {
        StringBuilder builder = new StringBuilder();
        builder.append("select * from (select A.*,ROWNUM as id FROM ( ");
        builder.append("select v3.* from (");
        builder.append("select sc.* from " + this.RmaOrderTbale + " sc where sc.created_by = " + operatorId + "");
        builder.append(" or sc.accepting_network_id in (");
        builder.append("select v2.customer_id from " + this.RmaOperatorCustomerTable + " v2 where v2.operatorid = " + operatorId + ")");
        builder.append(") v3, " + this.RmaMaterialsTable + " cp ");
        builder.append("where 1=1  and v3.code_confirm = cp.material_code(+) and v3.SERVICE_TYPE like '%服务站维修%' and v3.INVOICES_STATUS!='F' ");
        if (StringUtils.isNotBlank(queryString)) {
            builder.append(" and (v3.customer_name like '%" + queryString + "%' or v3.code_confirm like '%" + queryString + "%')");
        }
        builder.append(" ) A WHERE ROWNUM <= " + page + " * " + limit + " ) WHERE id > (" + page + " - 1) *" + limit);

        // 统计语句
        StringBuilder countBuilder = new StringBuilder();
        countBuilder.append("select count(1) from (");
        countBuilder.append("select sc.* from " + this.RmaOrderTbale + " sc where sc.created_by = " + operatorId + "");
        countBuilder.append(" or sc.accepting_network_id in (");
        countBuilder.append("select v2.customer_id from " + this.RmaOperatorCustomerTable + " v2 where v2.operatorid = " + operatorId + ")");
        countBuilder.append(") v3, " + this.RmaMaterialsTable + " cp ");
        countBuilder.append("where 1=1  and v3.code_confirm = cp.material_code(+)");
        if (StringUtils.isNotBlank(queryString)) {
            countBuilder.append(" and (v3.customer_name like '%" + queryString + "%' or v3.code_confirm like '%" + queryString + "%')");
        }
        List<Rma> list = olDataSource.queryForList(Rma.class, builder.toString());
        int total = olDataSource.getCount(countBuilder.toString());
        Pager<Rma> pager = new Pager<Rma>();
        pager.setPagerList(list);
        pager.setTotalRow(total);
        return pager;
    }

    @Override
    public String findOperatorByUserId(String userId) throws Exception {
        StringBuilder builder = new StringBuilder();
        builder.append("select o.operatorId from " + this.operatorTable + " o where upper(o.userId) = upper('" + userId + "')");
        // String operatorId = (String) olDataSource.queryForObject(builder.toString(), 1);
        Object operatorId = olDataSource.queryForObject(builder.toString(), 1);
        return operatorId.toString();
    }

}
